﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Utility;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;

namespace Portal.Systems.Render
{
    public class SystemLogRenderBll
    {
        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                DateTime dFromDate=new System.DateTime(System.DateTime.Now.Year,System.DateTime.Now.Month,1);
                DateTime dToDate=System.DateTime.Now;
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ProcessCheck(LogAccessId)\r\n"+
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        Checked=document.getElementById('chk'+LogAccessId).checked;\r\n"+
                   "        CheckedIds=document.getElementById('txtCheckedIds').value;\r\n"+
                   "        AjaxOut = Portal.Systems.Render.SystemLogRenderBll.ServerSideProcessChecked(SiteId,CheckedIds,LogAccessId,Checked).value;\r\n" +
                   "        document.getElementById('txtCheckedIds').value=AjaxOut.HtmlContent;\r\n"+
                   "    }\r\n"+

                   "    function ClientDelete()\r\n"+
                   "    {\r\n"+
                   "        CheckedIds=document.getElementById('txtCheckedIds').value;\r\n"+
                   "        if(CheckedIds=='')\r\n"+
                   "        {\r\n"+
                   "            alert('Chưa đánh dấu vào log cần xóa!');\r\n"+
                   "            return;\r\n"+
                   "        }\r\n"+
                   "        if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n"+
                   "        AjaxOut = Portal.Systems.Render.SystemLogRenderBll.ServerSideDelete(SiteId,CheckedIds).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n"+
                   "        {\r\n"+
                   "            alert(AjaxOut.InfoMessage);\r\n"+
                   "            return;\r\n"+
                   "        }\r\n"+
                   "        ClientReading(0);\r\n"+
                   "    }\r\n"+

                   "    function ClientReading(PageIndex)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        fromDate = document.getElementById('txtFromDate').value;\r\n"+
                   "        toDate = document.getElementById('txtToDate').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.SystemLogRenderBll.ServerSideReading(SiteId, fromDate, toDate, PageIndex).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListSystemLog').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +
                   "</script>\r\n" +
                   "<input id=txtCheckedIds type=hidden value=\"\">\r\n"+
                   "<div id=divActionForm class=CssFormSaveUpdate style=\"display:none;\"></div>\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientReading(0);\"><img class=image src=\""+ButtonIconRoot+"/Refresh.png\"><div>Đọc lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientDelete();\"><img class=image src=\""+ButtonIconRoot+"/Delete.png\"><div>Xoá log đánh dấu</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <h3>Nhật ký thao tác của người sử dụng</h3>\r\n"+
                   "  <div class=CssFilter>Từ ngày: "+LookUpUtilities.GenerateLookUpDate("txtFromDate",dFromDate)+" đến ngày "+LookUpUtilities.GenerateLookUpDate("txtToDate",dToDate)+"</div>\r\n"+
                   "  <div id=\"divListSystemLog\" class=tabcontent>\r\n" +
                         ServerSideReading(SiteId,dFromDate.ToString("dd/MM/yyyy"),dToDate.ToString("dd/MM/yyyy"), 0).HtmlContent+
                   "  </div>\r\n" +
                   "</div>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>"+ ex.Message.ToString()+"</div>\r\n";
            }
            
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId,string FromDate,string ToDate,int CurrentPageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemLogCls[] SystemLogs = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingSystemLog(SiteId, FromDate, ToDate);
                ReturnPaging OReturnPaging= WebPaging.GetPaging(SystemLogs.Length, CurrentPageIndex, 50, 10, "ClientReading");
                string Html =
                    OReturnPaging.PagingText+
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex></th>\r\n" +
                   "                    <th class=thIndex>#</th>\r\n" +
                   "                    <th class=thCode>Ngày</th>\r\n" +
                   "                    <th>Mã</th>\r\n" +
                   "                    <th>Log truy cập</th>\r\n" +
                   "                    <th>Tài khoản</th>\r\n" +
                   "                    <th>Họ tên</th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                {
                    Html +=
                       "    <tr>\r\n" +
                       "        <td><input type=checkbox style=\"cursor:pointer;\" id=\"chk"+SystemLogs[iIndex].LogAccessId+"\" onchange=\"javascript:ProcessCheck('"+SystemLogs[iIndex].LogAccessId+"');\"></td>\r\n"+
                       "        <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                       "        <td>" + SystemLogs[iIndex].LogAccessDate.ToString("dd/MM/yyyy HH:mm") + "</td>\r\n" +
                       "        <td>" + SystemLogs[iIndex].Action + "</td>\r\n" +
                       "        <td style=\"width:400px;\">" + SystemLogs[iIndex].ActionText + "</td>\r\n" +
                       "        <td>" + SystemLogs[iIndex].LoginName + "</td>\r\n" +
                       "        <td>" + SystemLogs[iIndex].FullName + "</td>\r\n" +
                       "   </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDelete(string SiteId, string CheckedIds)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CheckedIds = CheckedIds.Replace("$", "");
                string[] Items = CheckedIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).Delete(SiteId, Items);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideProcessChecked(
            string SiteId, 
            string CheckedIds,
            string Id,
            bool Checked)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (Checked)
                {
                    CheckedIds = CheckedIds+"$"+Id + "$;";
                }
                else
                {
                    CheckedIds = CheckedIds.Replace("$" + Id + "$", "");
                }
                CheckedIds = CheckedIds.Trim();
                int iFound = CheckedIds.IndexOf(";;");
                while (iFound != -1)
                {
                    CheckedIds = CheckedIds.Replace(";;", ";");
                    iFound = CheckedIds.IndexOf(";;");
                }
                if (CheckedIds.Length > 0)
                {
                    if (CheckedIds[0].ToString().Equals(";"))
                    {
                        CheckedIds = CheckedIds.Substring(1);
                    }
                }
                CheckedIds = CheckedIds.Trim();
                OAjaxOut.HtmlContent = CheckedIds;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

    }
}
